<?php
class ArticleController extends FrontController {
	function ArticleController(){
		parent::FrontController();
	}
	
	public function index(){
		
	}
	
	public function search(){
		
	}
	
	public function cate(){
		global $cate; //可以提供给控件访问
		$mod = new Article_cateModel();
		$cate = $mod->get_by_key_site_limit($this->id);
		$this->assign('cate', $cate);

		//加载当前路径 
		$this->get_breadcrumbs($this->id);

		$this->init_seo($cate);
		$this->init_page_info();
		
		$condition = array('site_id'=>SITE_ID, 'cate'=>$this->id);
		//加载文章列表
		$article_mod = new ArticleModel();
		$total = $article_mod->count($condition);
		$this->set_page_total($total);
		if($total>0){
			$articles = $article_mod->find($condition);
			$url = $cate['path'];
			foreach ($articles as &$article){
				init_article_url($article, $url);
			}
			$this->assign('articles', $articles);
		}
		
		//获取路径 
		if(isset($cate['template_list']) && !empty($cate['template_list'])){
			$tpl = $cate['template_list'];
		}else{
			$tpl = 'article_list';
		}
		$this->display($tpl);
	}
	
	public function view(){
		$mod = new ArticleModel();
		$article = $mod->get_by_key_site_limit($this->id);
		
		$this->init_seo($article);
		
		
		if(isset($article['cate'])){
			$cate_id = $article['cate'];
			if(is_array($cate_id)){
				$cate_id = $cate_id[0];
			}
			$this->get_breadcrumbs($cate_id );
		}
		
		//获取路径 
		if(isset($article['template']) && !empty($article['template'])){
			$tpl = $article['template'];
		}else{
			$tpl = 'article';
		}
		$this->assign('article', $article);
		$this->display($tpl);
	}
	
	/**
	 * @param cates
	 * @param breadcrumbs
	 * @param cate_id
	 */
	private function get_breadcrumbs($cate_id) {
		$breadcrumbs = array();
	 	$cate_mod = new Article_cateModel();
	 	$cates = $cate_mod->get_cates(SITE_ID, 'name,parent,path,alias');
		global $cate; //可以提供给控件访问
		if($cate === null){
			$cate = $cates[$cate_id];
		}
		while (true){
			if(isset($cates[$cate_id])){
				$cates[$cate_id]['url'] = $cates[$cate_id]['path'] . '/';
				$breadcrumbs[] = $cates[$cate_id];
			}
			if(!isset($cates['parent'])){
				break;
			}
				
			$cate_id = $cates['parent'];
		}
		$this->assign('breadcrumbs', $breadcrumbs);}

}

?>